*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear
keep if !mi(ideology)

*******************************************************************************
* Rescale ideology and presidential vote
*******************************************************************************

su ideology
replace ideology = (ideology - r(min))/(r(max) - r(min))
su trump
replace trump = (trump - r(min))/(r(max) - r(min))

*******************************************************************************
* Make interactions
*******************************************************************************

gen lpop = log(population + 1)
xtile qpop = lpop, nq(2)
//replace qpop = lpop
gen deputyXqpop = deputy*qpop
gen crime = property + violent
gen crimepcp = crime/population*100000
xtile qcrime = crimepcp, nq(2)
//replace qcrime = crimepcp
gen deputyXqcrime = deputy*qcrime

*******************************************************************************
* Interaction with population
*******************************************************************************

eststo clear

eststo: areg voteshare deputy qpop deputyXqpop cycle_* i.ncandidates, cl(district) a(state)
eststo: areg voteshare deputy qpop deputyXqpop ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg voteshare deputy qpop deputyXqpop ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

eststo: areg winner deputy qpop deputyXqpop cycle_* i.ncandidates, cl(district) a(state)
eststo: areg winner deputy qpop deputyXqpop ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner deputy qpop deputyXqpop ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable qpop "Population"
label variable deputyXqpop "Experienced X Population"
label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable ideology "Candidate Conservatism"

#delimit;

esttab
	using "_output/tableS7_a.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* Interaction with crime
*******************************************************************************

eststo clear

eststo: areg voteshare deputy qcrime deputyXqcrime cycle_* i.ncandidates, cl(district) a(state)
eststo: areg voteshare deputy qcrime deputyXqcrime ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg voteshare deputy qcrime deputyXqcrime ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

eststo: areg winner deputy qcrime deputyXqcrime cycle_* i.ncandidates, cl(district) a(state)
eststo: areg winner deputy qcrime deputyXqcrime ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner qcrime deputyXqcrime ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable qcrime "Crime"
label variable deputyXqcrime "Experienced X Crime"

#delimit;

esttab
	using "_output/tableS7_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
